package com.br.android.widing;

import java.text.DecimalFormat;
import java.util.Currency;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widing.db.WidingDAO;

public class ExibicaoItemActivity extends SherlockActivity{

	private TextView textViewValor;
	private TextView textViewNomeGasto;
	private TextView textViewCategoria;
	private TextView textViewData;

	private DecimalFormat format;

	private WidingDAO dao;

	//id do gasto que poder� ser exclu�do
	private int idGasto;

	private Intent intentGlobal;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_exibicao_item);

		Currency currency = Currency.getInstance(Locale.getDefault());
		format = new DecimalFormat(currency.getSymbol() + " ###,###,##0.00");

		textViewValor = (TextView) findViewById(R.id.textViewExibicaoValorGasto);
		textViewNomeGasto = (TextView) findViewById(R.id.textViewExibicaoNomeGasto);
		textViewCategoria = (TextView) findViewById(R.id.textViewExibicaoCategoria);
		textViewData = (TextView) findViewById(R.id.textViewExibicaoData);
		
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayoutExibicaoGasto);
		BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(R.drawable.bg_striped_img);
		bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		layout.setBackgroundDrawable(bg);

		dao = new WidingDAO(this);

		intentGlobal = getIntent();
		idGasto = intentGlobal.getExtras().getInt("id");
		textViewValor.setText(format.format(intentGlobal.getExtras().getDouble("valor")));
		textViewNomeGasto.setText(intentGlobal.getExtras().getString("nomegasto"));
		textViewCategoria.setText(intentGlobal.getExtras().getString("categoria"));
		textViewData.setText(intentGlobal.getExtras().getString("data"));

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_exibicao_item, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_editar_item:
			Intent intent = new Intent(this, EditGastoActivity.class);
			intent.putExtra("id", idGasto);
			intent.putExtra("valor", intentGlobal.getExtras().getDouble("valor"));
			intent.putExtra("nomegasto", intentGlobal.getExtras().getString("nomegasto"));
			intent.putExtra("categoria", intentGlobal.getExtras().getString("categoria"));
			intent.putExtra("data", intentGlobal.getExtras().getString("data"));

			startActivity(intent);
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		case R.id.menu_excluir_item:
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle(textViewNomeGasto.getText().toString());
			dialog.setMessage("Tem certeza que deseja excluir este gasto?");

			dialog.setPositiveButton("Sim", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dao.excluirGasto(idGasto);
					startActivity(new Intent(ExibicaoItemActivity.this, PrincipalActivity.class));
					overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
					mensagem("Gasto exclu\u00eddo com sucesso.");
					finish();
					dao.close();
				}
			});

			dialog.setNegativeButton("Cancelar", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});

			dialog.show();
			break;
		default:
			startActivity(new Intent(this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void mensagem(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(event.getAction() == KeyEvent.ACTION_DOWN){
			startActivity(new Intent(this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
